﻿package com.om.common;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;



/**
 *时间戳 与 时间字符串相互转换
 * 
 * @author admin
 * 
 */
public class TimeStampToInt {

	public static int nowTimestamp() {
		return TimeStampToInt.timeToInteger(TimeStampToInt.systemTime(),
				ConstantsUtil.YYYY_MM_DD_HHMMSS);
	}

	/**
	 * 将时间转化为时间戳
	 * 
	 * @param time
	 *            时间字符串，如：2011-01-22 10：10：10
	 * @param format
	 *            时间格式 如： yyyy-MM-dd HH:mm:ss
	 * @return int 时间戳
	 */
	public static int timeToInteger(String time, String format) {
		int nRet = 0;
		
		try {
			java.text.SimpleDateFormat dateFormat=null;
			if(time.length()<19){
				dateFormat = new java.text.SimpleDateFormat(
						"yyyy-MM-dd");
			}else{
				 dateFormat = new java.text.SimpleDateFormat(
						format);
			}
			Date de = dateFormat.parse(time);
			nRet = (int) (de.getTime() / 1000);
		} catch (Exception e) {/* DISCARD EXCEPTION */
			e.printStackTrace();
		}
		return nRet;
	}

	/**
	 * 时间戳转换成String
	 * 
	 * @param time
	 *            时间戳
	 * @return String 時間字符串 格式： yyyy-MM-dd HH:mm:ss
	 */
	public static String timeToString(String time) {
		Long timestamp = Long.parseLong(time) * 1000;
		String date = new java.text.SimpleDateFormat(ConstantsUtil.YYYY_MM_DD_HHMMSS).format(new java.util.Date(timestamp));
		return date;
	}
	
	
	@SuppressWarnings("deprecation")
	public static String timeToFormate(String time,String formate) {
		String date ="";
		try{
		SimpleDateFormat bartDateFormat =   new SimpleDateFormat(formate);  
		Date dt = bartDateFormat.parse(time);  
		date = new java.text.SimpleDateFormat(formate).format(dt);
		}catch(Exception ex){
			
			ex.printStackTrace();
		
		}
	
		return date;
	}
	
	public static String timeToFormate(String time,String fromFormat,String targetFormat) {
		String date ="";
		try{
		SimpleDateFormat bartDateFormat =   new SimpleDateFormat(fromFormat);  
		Date dt = bartDateFormat.parse(time);  
		date = new java.text.SimpleDateFormat(targetFormat).format(dt);
		}catch(Exception ex){
			
			ex.printStackTrace();
		
		}
	
		return date;
	}

	/**
	 * 时间戳转换成String
	 * 
	 * @param time
	 *            时间戳
	 * @param format
	 *            格式
	 * @return String 時間字符串
	 */
	public static String timeToStr(String time, String format) {
		Long timestamp = Long.parseLong(time) * 1000;
		String date = new java.text.SimpleDateFormat(format)
				.format(new java.util.Date(timestamp));
		return date;
	}

	/**
	 * 获取当前系统时间 输出这种形式 2008-08-08 08:08:08
	 */

	public static String systemTime() {
		String timeZone = ConfigReader.GetProptery("time_zone");
		Calendar c = Calendar.getInstance();
		SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		if(!timeZone .equals("")){
			TimeZone tz =TimeZone.getTimeZone(timeZone);
			TimeZone.setDefault(tz);
			c = Calendar.getInstance(TimeZone.getTimeZone( timeZone));
		}
		else{
			c = Calendar.getInstance(Locale.getDefault());
		}
		return simpleDateTimeFormat.format(c.getTime());
	}

	public static String Postime(String time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			Date date = df.parse(time);
			return date.toLocaleString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取本周一时间
	 * 
	 */
	public static String getMonday1() {

		Date date = new Date();
		Calendar calendar = new GregorianCalendar();

		// 取得本周一
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1 = df.format(calendar.getTime());
		// System.out.println(df.format(calendar.getTime()));
		return date1;

	}

	/**
	 * 获取本周日时间
	 * 
	 */
	public static String getMonday7() {

		Date date = new Date();
		Calendar calendar = new GregorianCalendar();

		// 取得本周一
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String date7 = df.format(new Date(calendar.getTime().getTime()
				+ (7 * 24 * 60 * 60 * 1000) - 1000));
		// System.out.println(date7);
		return date7;
	}

	
	/***
	 * 获取一周前的时间
	 * @return 一周前的时间
	 */
	public static String lastWeek() {

		Date date = new Date();

		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));

		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));

		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - 6;

		if (day < 1) {

			month -= 1;

			if (month == 0) {

				year -= 1;
				month = 12;

			}

			if (month == 4 || month == 6 || month == 9 || month == 11) {

				day = 30 + day;

			} else if (month == 1 || month == 3 || month == 5 || month == 7
					|| month == 8 || month == 10 || month == 12) {

				day = 31 + day;

			} else if (month == 2) {

				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {

					day = 29 + day;

				}

				else {

					day = 28 + day;

				}

			}

		}

		String y = year + "";
		String m = "";
		String d = "";

		if (month < 10) {

			m = "0" + month;

		} else {

			m = month + "";

		}

		if (day < 10) {

			d = "0" + day;

		} else {

			d = day + "";

		}

		return y + "-" + m + "-" + d + " " + "00:00:00 ";

	}

	/**
	 * 本月最后一天
	 * @return 本月最后一天
	 */
	public static String getEndMonthDay() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		String str = formatter.format(cal.getTime());
		return str + " 23:59:59";
	}

	/**
	 * 本月第一天
	 * 
	 * @return 本月第一天
	 */
	public static String getStratMonthDay() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		int value = cal.getGreatestMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		String str = formatter.format(cal.getTime());
		return str + " 00:00:00";
	}

	// 获得上月第一天
	/**
	 * 获得上月第一天
	 * @return 获得上月第一天
	 */
	public static String lastMonth() {

		Date date = new Date();

		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));

		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - 1;

		int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));

		if (month == 0) {
			year -= 1;
			month = 12;

		} else if (day > 28) {
			if (month == 2) {
				if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
					day = 29;
				} else {
					day = 28;
				}
			} else if ((month == 4 || month == 6 || month == 9 || month == 11)
					&& day == 31) {
				day = 30;
			}
		}
		String y = year + "";
		String m = "";
		String d = "";

		if (month < 10) {
			m = "0" + month;
		} else {
			m = month + "";
		}
		if (day < 10) {
			d = "0" + day;
		} else {
			d = day + "";
		}

		return y + "-" + m + "-" + "00" + " 00:00:00";

	}

	/**
	 * 上月的最后一天
	 * 
	 * @return 上月的最后一天
	 */
	public static String getLastMonthLastDay() {
		Date date = new Date();
		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
		int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - 1;
		int day = 0;

		if (month == 2) {
			if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
				day = 29;
			} else {
				day = 28;
			}
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			day = 30;
		}
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			day = 31;
		}
		String y = year + "";
		String m = "";
		String d = "";
		if (month < 10) {
			m = "0" + month;
		} else {
			m = month + "";
		}
		return y + "-" + m + "-" + day + " 00:00:00";

	}

	/**
	 * 返回某一时间的开始时间和结束时间
	 * 
	 * @param kinds
	 * 0 全部 1、最近一周 2、本月 3、上月
	 * @return 回某一时间的开始时间和结束时间
	 */
	public static Map<Object, Object> getQueryTime(int kinds) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (kinds == 0) {
			map.put("startTime", "none");
		}
		if (kinds == 1) {
			map.put("endTime", TimeStampToInt.systemTime());
			map.put("startTime", TimeStampToInt.lastWeek());

		}
		if (kinds == 2) {
			map.put("startTime", TimeStampToInt.getStratMonthDay());
			map.put("endTime", TimeStampToInt.getEndMonthDay());
		}

		if (kinds == 3) {
			map.put("startTime", TimeStampToInt.lastMonth());
			map.put("endTime", TimeStampToInt.getLastMonthLastDay());
		}

		return map;
	}

	/**
	 * 取几天前的日期
	 * 
	 * @param days 天数
	 * @return days天前的日期
	 */
	public static String getDay(int days) {
		Date d = new Date(System.currentTimeMillis() - days * 1000 * 60 * 60
				* 24);
		SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
		String day = sp.format(d);
		return day;
	}

	
}
